#pragma once
#include <vector>
#include <QString>
#include <QDomDocument>
#include <QMap>
#include "clcommap.h"

#define PXML_FILE_INVALID	-1
#define PXML_NO_VALUE		0
#define PXML_OK				1

#define XML_NAME_LEN	64
#define XML_ATTR_LEN	256
#define XML_VALUE_LEN	256
#define XML_ID_LEN	32

class CXMLTree
{
private:
	bool m_bVRoot;

    QString m_czID;
    map<std::string, CXMLTree*> m_mapIdChilds;
    map<QString, map<std::string, CXMLTree*>> m_mapNameChilds;
    QMap<QString, QString> m_mapAttributes;
    QString m_czValue;
    QString m_czName;

    CXMLTree* m_pFather;
public:
	string GetID();
public:
    CXMLTree(QString name, void* pXMLFile, CXMLTree* pfather, bool broot=false);
	CXMLTree& operator=(const CXMLTree& other)=delete;
	~CXMLTree();
	void AddChild(CXMLTree* pt);
    CXMLTree* AddChild(QString namestr, QString valuestr);
    CXMLTree* AddChild(QString namestr, int nvalue);
    CXMLTree* AddChild(QString namestr, double dvalue);
    QString GetName();
    QString GetValue();
    QString GetAttru(QString attname);
    QString GetAttru();
    int AttruCount();

    int SetValue(QString valuestr);
	int SetNValue(int nValue);
	int SetDValue(double dValue);
	int DelNode(string id);
    int DelNodeByName(QString name);
    int UpdateAttribute(QString attname, QString attributestr);

	int GetIntValue();
	double GetDoubleValue();
    QString GetCharsValue();

	CXMLTree* GetFirstNode(std::map<std::string, CXMLTree*>::iterator& it);
	CXMLTree* GetNextNode(std::map<std::string, CXMLTree*>::iterator& it);
	CXMLTree* GetNode(
		string id);
	CXMLTree* GetNodeByName(
        QString name);
	CXMLTree* GetFather();
public:
    friend void GetNodeStr(CXMLTree* pt, QString& str, int& nSpace);

private:
	void* _pXMLFile;
};
typedef CXMLTree TREE, *PTREE;

class CParseXMLConfigFile
{
public:
	CParseXMLConfigFile(string id);
	CParseXMLConfigFile();
	~CParseXMLConfigFile();
    int LoadXMLFile(QString fileName);
    bool CreateXMLFile(QString fileName);
	int Save2XMLFile();
    bool ReloadFromXMLFile();
public:
	string GetID();
public:
    QString GetConfigFileName();
    PTREE GetNodeByName(QString nodeName);
	PTREE GetRootNode();

	void ClearIDMaps();
	void Add2IDMaps(string id, PTREE pt);
	void DelPtree(string id);

	PTREE GetNodeByID(string id);
private:
    void EnumXml(QDomDocument& xml);
    void parseNode(void* pXMLFile, CXMLTree* pfather, const QDomNode& node);
    void parseAttribute(
            PTREE ptree,
            const QDomNamedNodeMap& attributemap);
    QString	m_strFileName;

	PTREE m_pRoot;

	map<string, PTREE> _mapIDTrees;
private:
	char czID[XML_ID_LEN];
};
